package com.supreme.aspectserver;

import com.supreme.aspectserver.props.AspectServerProps;
import com.supreme.common.constant.ConsoleColors;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Optional;

@SpringBootApplication
public class SupremeAspectServerApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(SupremeAspectServerApplication.class, args);
        System.out.println("\n\n\t\t ↖(▔▽▔)↗ SupremeAspectServerApplication 启动完成 ↖(▔▽▔)↗ \n");
        System.out.println("    _    ____  ____  _____ ____ _____   ____  _____ ______     _______ ____\n" +
                "   / \\  / ___||  _ \\| ____/ ___|_   _| / ___|| ____|  _ \\ \\   / / ____|  _ \\\n" +
                "  / _ \\ \\___ \\| |_) |  _|| |     | |   \\___ \\|  _| | |_) \\ \\ / /|  _| | |_) |\n" +
                " / ___ \\ ___) |  __/| |__| |___  | |    ___) | |___|  _ < \\ V / | |___|  _ <\n" +
                "/_/   \\_\\____/|_|   |_____\\____| |_|   |____/|_____|_| \\_\\ \\_/  |_____|_| \\_\\");

        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String buildVersion = environment.getProperty("maven-project.version");
        System.out.println("BUILD_VERSION = " + buildVersion);

        String buildDate = environment.getProperty("maven-project.buildDate");
        Optional.ofNullable(buildDate).ifPresent(present -> {
            Instant instant = LocalDateTime.parse(
                    present, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
            ).toInstant(ZoneOffset.UTC);
            String localDatetime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            System.out.println("BUILD_DATETIME = " + localDatetime);
        });

        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        AspectServerProps aspectServerProps = beanFactory.getBean("aspectServerProps", AspectServerProps.class);
        ConsoleColors.printf(ConsoleColors.YELLOW_BOLD_BRIGHT, "startCommand: " + aspectServerProps.getStartCommand());
        ConsoleColors.printf(ConsoleColors.GREEN_BOLD_BRIGHT, "tempContent: " + aspectServerProps.getTempContent());

        System.out.println("\n========== SYSTEM ENV ==========");
        Map<String, String> systemEnv = System.getenv();
        String aspectController = systemEnv.get("ASPECT_CONTROLLER");
        System.out.println("ASPECT_CONTROLLER = " + aspectController);
        String aspectService = systemEnv.get("ASPECT_SERVICE");
        System.out.println("ASPECT_SERVICE = " + aspectService);
        System.out.println("========== SYSTEM ENV ==========\n");
    }

}
